package com.office.web.controller.system;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.office.common.annotation.Log;
import com.office.common.core.controller.BaseController;
import com.office.common.core.domain.AjaxResult;
import com.office.common.enums.BusinessType;
import com.office.system.domain.SysReportMaterial;
import com.office.system.service.ISysReportMaterialService;
import com.office.common.utils.poi.ExcelUtil;
import com.office.common.core.page.TableDataInfo;

/**
 * 报告素材Controller
 */
@RestController
@RequestMapping("/system/material")
public class SysReportMaterialController extends BaseController {
    @Autowired
    private ISysReportMaterialService sysReportMaterialService;

    /**
     * 查询报告素材列表
     */
    @PreAuthorize("@ss.hasPermi('system:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysReportMaterial sysReportMaterial) {
        startPage();
        List<SysReportMaterial> list = sysReportMaterialService.selectSysReportMaterialList(sysReportMaterial);
        return getDataTable(list);
    }

    /**
     * 导出报告素材列表
     */
    @PreAuthorize("@ss.hasPermi('system:material:export')")
    @Log(title = "报告素材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysReportMaterial sysReportMaterial) {
        List<SysReportMaterial> list = sysReportMaterialService.selectSysReportMaterialList(sysReportMaterial);
        ExcelUtil<SysReportMaterial> util = new ExcelUtil<SysReportMaterial>(SysReportMaterial.class);
        util.exportExcel(response, list, "报告素材数据");
    }

    /**
     * 获取报告素材详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:material:query')")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId) {
        return success(sysReportMaterialService.selectSysReportMaterialByMaterialId(materialId));
    }

    /**
     * 新增报告素材
     */
    @PreAuthorize("@ss.hasPermi('system:material:add')")
    @Log(title = "报告素材", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysReportMaterial sysReportMaterial) {
        return toAjax(sysReportMaterialService.insertSysReportMaterial(sysReportMaterial));
    }

    /**
     * 修改报告素材
     */
    @PreAuthorize("@ss.hasPermi('system:material:edit')")
    @Log(title = "报告素材", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysReportMaterial sysReportMaterial) {
        return toAjax(sysReportMaterialService.updateSysReportMaterial(sysReportMaterial));
    }

    /**
     * 删除报告素材
     */
    @PreAuthorize("@ss.hasPermi('system:material:remove')")
    @Log(title = "报告素材", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialIds}")
    public AjaxResult remove(@PathVariable Long[] materialIds) {
        return toAjax(sysReportMaterialService.deleteSysReportMaterialByMaterialIds(materialIds));
    }

    /**
     * 根据日期范围查询素材
     */
    @PreAuthorize("@ss.hasPermi('system:material:query')")
    @GetMapping("/listByDateRange")
    public AjaxResult listByDateRange(String startDate, String endDate) {
        List<SysReportMaterial> list = sysReportMaterialService.selectSysReportMaterialByDateRange(startDate, endDate);
        return success(list);
    }

    /**
     * 获取素材统计信息
     */
    @GetMapping("/statistics")
    public AjaxResult getStatistics(String startDate, String endDate) {
        try {
            // 这里实现素材统计逻辑
            java.util.Map<String, Object> statistics = new java.util.HashMap<>();
            statistics.put("totalCount", 0);
            statistics.put("weeklyCount", 0);
            statistics.put("monthlyCount", 0);
            statistics.put("categoryStats", new java.util.HashMap<>());
            
            return success(statistics);
        } catch (Exception e) {
            return error("获取统计信息失败：" + e.getMessage());
        }
    }

    /**
     * 批量操作素材
     */
    @PreAuthorize("@ss.hasPermi('system:material:edit')")
    @Log(title = "批量操作素材", businessType = BusinessType.UPDATE)
    @PostMapping("/batch")
    public AjaxResult batchOperate(@RequestBody java.util.Map<String, Object> params) {
        try {
            String operation = (String) params.get("operation");
            java.util.List<Long> materialIds = (java.util.List<Long>) params.get("materialIds");
            
            // 这里实现批量操作逻辑
            switch (operation) {
                case "delete":
                    return toAjax(sysReportMaterialService.deleteSysReportMaterialByMaterialIds(materialIds.toArray(new Long[0])));
                case "export":
                    // 实现批量导出逻辑
                    return success("批量导出成功");
                default:
                    return error("不支持的操作类型");
            }
        } catch (Exception e) {
            return error("批量操作失败：" + e.getMessage());
        }
    }
}
